<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/28
 * Time: 16:36
 */

namespace api\storage\service;


use api\storage\model\SeatModel;
use api\storage\model\StorageModel;

class SeatService
{
    /**
     * 一块板的数量。默认为：28
     */
    const ONE_EQUIPMENT_LIGHT_COUNT = 28;

    /**
     * 绑定一块设备
     *
     * @param $startCode
     * @param $endCode
     * @param $equipCode
     * @param $uuid
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function bindSeat($startCode, $endCode, $equipCode, $uuid)
    {
        $startNum = intval(substr($startCode, -3));
        $endNum = intval(substr($endCode, -3));
        $length = abs($endNum - $startNum) + 1;
        if ($length > self::ONE_EQUIPMENT_LIGHT_COUNT) {
            return -1;
        }
        //seat的前四位。seatId 有6位组成，前4位是设备码，后2位是灯的位置
        $head = str_pad(strtoupper(dechex($equipCode)), 4, 0, STR_PAD_LEFT);
        $seatModel = new SeatModel();
        $seatModel->startTrans();
        for ($i = 0; $i < $length; $i++) {
            $position = str_pad(strtoupper(dechex($i)), 2, 0, STR_PAD_LEFT);
            $save['seat_id'] = $head . $position;
            $res = $seatModel->updateSeatId($startCode++, $uuid, $save);
            if ($res === false) {
                $seatModel->rollback();
                return 0;
            }
        }
        $seatModel->commit();
        $this->changeStorageType($uuid);
        return 1;
    }

    /**
     * 绑定多块设备
     *
     * @param $startCode
     * @param $endCode
     * @param $equipCodes
     * @param $uuid
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function bindMultiSeat($startCode, $endCode, $equipCodes, $uuid)
    {
        $startNum = intval(substr($startCode, -3));
        $endNum = intval(substr($endCode, -3));
        $length = abs($endNum - $startNum) + 1;
        if ($length > (self::ONE_EQUIPMENT_LIGHT_COUNT * count($equipCodes)) ) {
            return -1;
        }
        $seatModel = new SeatModel();
        $seatModel->startTrans();
        for ($i = 0; $i < count($equipCodes); $i++) {
            //seat的前四位。seatId 有6位组成，前4位是设备码，后2位是灯的位置
            $len = self::ONE_EQUIPMENT_LIGHT_COUNT;
            $head = str_pad(strtoupper(dechex($equipCodes[$i])), 4, 0, STR_PAD_LEFT);
            for ($j = 0; $j < $len; $j++) {
                $position = str_pad(strtoupper(dechex($j)), 2, 0, STR_PAD_LEFT);
                $save['seat_id'] = $head . $position;
                $res = $seatModel->updateSeatId($startCode++, $uuid, $save);
                if ($res === false) {
                    $seatModel->rollback();
                    return 0;
                }
            }
        }
        $seatModel->commit();
        $this->changeStorageType($uuid);
        return 1;
    }

    /**
     * 转换仓库的类型
     *
     * @param $storageUuid
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function changeStorageType($storageUuid){
        $storageModel = new StorageModel();
        $storage = $storageModel->findStorageByUuid($storageModel);
        if ($storage != null && !empty($storage->getData()) ){
            //如果不是有灯的仓库，那么将其置为有灯的仓库
            if ($storage->getData('type') == 0) {
                $storageModel->updateType(1,$storageUuid);
            }
        }
    }
}